var itemSearch;
require(['grid', 'topBar', 'layout'], function () {
    var $gridList = $('#grid');
    var postData = {};
    $('#top').topBar({
        left: [{
            type: 'filter',
            search: function () {
                if (getPostData()) {
                    $gridList.grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '维保类型',
                    name: 'maintenCgy',
                    type: 'item-select',
                    dict: 'MAIN1000'
                },
                {
                    text: '剩余里程',
                    name: 'remainMile',
                    type: 'item-select',
                    data: [
                        {text: '≤100km', value: '100'},
                        {text: '≤300km', value: '300'},
                        {text: '≤500km', value: '500'},
                        {text: '≤1000km', value: '1000'}
                    ]
                },
                {
                    text: '剩余天数',
                    name: 'remainDay',
                    type: 'item-select',
                    data: [
                        {text: '≤5天', value: '5'},
                        {text: '≤10天', value: '10'},
                        {text: '≤20天', value: '20'},
                        {text: '≤30天', value: '30'}
                    ]
                }
            ]
        }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                        	exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                        	exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });
    $('body').layout();

    $gridList.grid({
        url: "/reportCarMaintePlan/getMaintenancePlanPage.htm",
        multiselect: false,
        colModel: [
            {name: "maintenId", index: "maintenId", label: 'maintenId', key: true, hidden: true},
            {name: "licPltNo", index: "licPltNo", label: '车牌号'},
            {name: "totalMile", index: "totalMile", label: '当前运行里程'},
            {name: "lastMaintenDate", index: "lastMaintenDate", label: '上次维保日期'},
            {name: "lastMaintenMile", index: "lastMaintenMile", label: '上次维保里程'},
            {name: "maintenCgy", index: "maintenCgy", label: '计划维保类别'},
            {name: "planMile", index: "planMile", label: '计划维保里程'},
            {name: "remainMile", index: "remainMile", label: '剩余里程'},
            {name: "planMaintenDate", index: "planMaintenDate", label: '计划维保日期'},
            {name: "remainTime", index: "remainTime", label: '剩余天数'},
            {name: "partsCode", index: "partsCode", label: '建议更换零部件'}
        ]
    });

    function exportFile(suffix){
    	if(getPostData()){                 		
    		var url = "/reportCarMaintePlan/exportMaintenancePlan"+suffix+".htm";
			$.exportFile(url,postData);
	    }
    }
    
    //获取需要传递到后台的参数
    function getPostData() {
        var type = commonFun.getLeftTreeCurrentTreeType();
        if (type == 'DRIVER') {
            type = 'CAR'
        }
        var carIds = commonFun.getLeftTreeCheckedNodes(type);
        if (!carIds.length) {
            $.messager.alert("提示", "请先选择左侧树的一辆车辆!", "warning");
            return false;
        }
        postData = $('#top').parent().getValues();
        postData.type = type;
        postData.carIds = carIds.toString();
        return true;
    }
    
    itemSearch = function(data){
    	$gridList.grid('setGridParam', {
            datatype: "json",
            postData: {
            	"orgIds": data.lineId,
            	"type": "LINE"
            }
        }).trigger("reloadGrid");
    }

});